//  QTI to HTML converter
//
//	Copyright (c) 2009, Krzysztof Langner
//	This code is released under MIT license

$import( "../libs/mootools-1.2.2-core.js" );
$import( "choiceInteraction.js" );
$import( "feedback.js" );

/**
 * @TODO Strona projektowa
 */
//-------------------------------------------------------------------------------------------------
function qth_convert(){
	
	// convert modules
	_qth.convert("choiceInteraction", create_choice);
	_qth.convert("feedback", create_feedback);
}

//-------------------------------------------------------------------------------------------------
function qth_check(){
	var max = 0;
	var result = 0;
	
	// Calculate test score
	_qth.modules.each(function(item, index){
		if(item.maxScore){
			max += item.maxScore;
			result += item.result;
		}
	});
	
	// show global feedback
	_qth.modules.each(function(item, index){
		if(item.showFeedback){
			item.showFeedback(result, max);
		}
	});
}

//-------------------------------------------------------------------------------------------------
function qth_reset(){

	_qth.modules.each(function(item, index){
		if(item.reset){
			item.reset();
		}
	});
}

//-------------------------------------------------------------------------------------------------
function qth_show_answers(){

	_qth.modules.each(function(item, index){
		if(item.showAnswers){
			item.showAnswers();
		}
	});
}

//-------------------------------------------------------------------------------------------------
//singleton object for qth engine
// Module types
var _qth = new function() {
 this.modules = new Array();

 this.convert = function (nodeName, fun) {
	var nodeList = document.getElementsByTagName(nodeName);
    for(var i = nodeList.length-1; i >= 0; i-- ){
    	obj = fun(nodeList[i]);
    	nodeList[i].parentNode.replaceChild(obj.getDOM(), nodeList[i]);
    	this.modules.push(obj);
    }
 };
};



//-------------------------------------------------------------------------------------------------
// Import javascript files into document
// Remember to change file_name if you change name of this file
function $import( path ){ 
 var base;
 var file_name = "qth.js";
 var scripts = document.getElementsByTagName("script"); 

 for ( var i = 0; i < scripts.length; i++ ){ 

 	if ( scripts[ i ].src.match( file_name ) ) { 
 		base = scripts[ i ].src.replace( file_name, "" ); 
 		break;
 	}
 }
 
 document.write("<" + "script type=\"text/javascript\" src=\"" + base + path + "\"></" + "script>"); 
}

